package com.android.adpater;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.ColorInt;
import androidx.annotation.ColorRes;
import androidx.annotation.NonNull;
import androidx.annotation.StringRes;

import com.android.adpater.interfaces.IAdapter;
import com.android.adpater.interfaces.IData;
import com.android.adpater.interfaces.OnClickListItemListener;

import java.util.ArrayList;
import java.util.List;


/**
 * @author GYL
 * @version 1.0
 * BaseAdapter 基类
 */
@SuppressWarnings({"unused", "WeakerAccess"})
public abstract class BaseAdapter<T> extends android.widget.BaseAdapter implements IData<T>, IAdapter<T> {

  private final Context mContext;
  private final List<T> mData;
  private boolean enableNotifyDataSetChanged=true;

  private OnClickListItemListener onItemViewClick;

  @Override
  public void setOnListItemClick(@NonNull OnClickListItemListener onItemViewClick) {
    this.onItemViewClick=onItemViewClick;
  }

  @Override
  public void addItemClick(@NonNull View view,final Object obj, @NonNull final int position) {
    view.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        if (onItemViewClick != null) {
          onItemViewClick.onPageViewItemClick(v, obj, position);
        }
      }
    });
  }

  public BaseAdapter(@NonNull Context context) {
    this(context, null);
  }

  public BaseAdapter(@NonNull Context context, List<T> data) {
    this.mData = data == null ? new ArrayList<T>() : new ArrayList<>(data);
    this.mContext = context;
  }

  @Override
  public String getString(@StringRes int strId, Object ... obj){
    if(obj==null){
      return mContext.getString(strId);
    }else{
      return mContext.getString(strId, obj);
    }
  }
  @Override
  public @ColorInt
  int getColor(@ColorRes int colorId){
    return AdapterViewHelper.getColor(mContext,colorId);
  }


  @Override
  public List<T> getData() {
    return mData;
  }

  @Override
  public int getCount() {
    return mData==null?0:mData.size();
  }

  @Override
  public T getItem(int position) {
    return position < 0 || position >= mData.size() ? null : mData.get(position);
  }

  @Override
  public long getItemId(int position) {
    return position;
  }

  @Override
  public View getView(int position, View convertView, ViewGroup parent) {
    final T item = getItem(position);
    final AdapterViewHelper helper = AdapterViewHelper.get(mContext,
        convertView,
        parent,
        getLayoutResId(this.getItemViewType(position)),
        position);

    onBindView(helper, item, position);
    helper.setAssociatedObject(item);
    return helper.getView();
  }

  @Override
  public int getItemViewType(T item, int position) {
    return super.getItemViewType(position);
  }

  @Override
  public int getItemViewType(int position) {
    //return super.getItemViewType(position);
    return getItemViewType(getItem(position),position);
  }

  @Override
  public int getViewTypeCount() {
    return super.getViewTypeCount();
  }

  @Override
  public void enableNotifyDataSetChanged(boolean enable) {
    this.enableNotifyDataSetChanged=enable;
  }

  @Override
  public boolean isEnabled(int position) {
    return position < mData.size();
  }

  @Override
  public void add(@NonNull T item) {
    mData.add(item);
    notifyDataSetChanged();
  }

  @Override
  public void insert(int position, @NonNull T item) {
    mData.add(position, item);
    notifyDataSetChanged();
  }

  @Override
  public void addAll(@NonNull List<T> list) {
    mData.addAll(list);
    notifyDataSetChanged();
  }

  @Override
  public void set(@NonNull T oldItem, @NonNull T newItem) {
    set(mData.indexOf(oldItem), newItem);
  }

  @Override
  public void set(int index, @NonNull T item) {
    if (index >= 0 && index < getCount()) {
      mData.set(index, item);
      notifyDataSetChanged();
    }
  }

  @Override
  public void remove(@NonNull T item) {
    mData.remove(item);
    notifyDataSetChanged();
  }

  @Override
  public void remove(int index) {
    if (index >= 0 && index < getCount()) {
      mData.remove(index);
      notifyDataSetChanged();
    }
  }

  @Override
  public void replaceAll(@NonNull List<T> item) {
    mData.clear();
    mData.addAll(item);
    notifyDataSetChanged();
  }

  @Override
  public boolean contains(@NonNull T item) {
    return mData.contains(item);
  }

  @Override
  public void clear() {
    mData.clear();
    notifyDataSetChanged();
  }


  @Override
  public void setData(@NonNull List<T> list) {
    if(list!=null){
      mData.clear();
      mData.addAll(list);
      notifyDataSetChanged();
    }
  }

  @Override
  public void addArray(@NonNull T[] datas) {
    if(datas!=null){
      for (T data:datas) {
        mData.add(data);
      }
      notifyDataSetChanged();
    }
  }

  @Override
  public void notifyDataSetChanged() {
    if(enableNotifyDataSetChanged){
      super.notifyDataSetChanged();
    }
  }
}
